package org.jactao.remote.netty.codec.marshalling;

import io.netty.handler.codec.marshalling.*;
import org.jboss.marshalling.*;

import java.io.IOException;

public final class MarshallingCodecFactory {

    /**
     * 创建Jboss MarshallingEncoder
     *
     * @return
     * @throws IOException
     */
    public static MarshallingEncoder buildMarshallingEncoder() {
        final MarshallerFactory marshallerFactory = Marshalling
                .getProvidedMarshallerFactory("serial");
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        MarshallerProvider marshallerProvider =
                new DefaultMarshallerProvider(marshallerFactory, configuration);
        return new MarshallingEncoder(marshallerProvider);
    }

    /**
     * 创建Jboss MarshallingDecoder
     *
     * @return
     * @throws IOException
     */
    public static MarshallingDecoder buildMarshallingDecoder() {
        final MarshallerFactory marshallerFactory = Marshalling
                .getProvidedMarshallerFactory("serial");
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        UnmarshallerProvider unmarshallerProvider =
                new DefaultUnmarshallerProvider(marshallerFactory, configuration);
        return new MarshallingDecoder(unmarshallerProvider);
    }
}